import Taro, { Component } from "@tarojs/taro";
import { connect } from "@tarojs/redux";
import { View } from "@tarojs/components";

import {
  onGetUserInfo,
  onConfirmUploadUserInfo,
  onConfirmLogin,
  onUploadUserInfo,
  onGetServerUserInfo
} from "../../actions/user";

import {put,get } from '../../utils/storage'

@connect(
  state => state.user,
  {
    onGetUserInfo,
    onConfirmUploadUserInfo,
    onConfirmLogin,
    onUploadUserInfo,
    onGetServerUserInfo
  }
)
class AuthRoute extends Component {
  componentDidMount() {
    if(get('login')){
      this.props.onGetServerUserInfo({openid:get('openid')})
    }
  }
  checkLoginState = () => {
    typeof this.props.uploadUserInfo !== "number"
      ? null
      :!this.props.login
      ?this.props.onConfirmUploadUserInfo()
      : (Taro.atMessage({
              message: this.props.specialWords.length===0?"登录成功":this.props.specialWords[0],
              info: "success",
              duration:this.props.specialWords.length===0?3000:10000
            }),
            this.props.onConfirmLogin())
  };

  shouldComponentUpdate(nextProps) {
    
    return nextProps.uploadUserInfo ===0||nextProps.uploadUserInfo ===1;
  }
  componentDidUpdate() {
      this.checkLoginState();
      let thisProps = this.props;
      !(thisProps.uploadUserInfo === 0) &&
      thisProps.login === false &&
      thisProps.userInfo &&
      thisProps.openid
        ? thisProps.onUploadUserInfo({
            userInfo: thisProps.userInfo,
            openid: thisProps.openid
          },()=>{
            put('login',true,3600*72)
            put('openid',thisProps.openid)
          })
        : null;
  }

  render() {
    
    return <View />;
  }
}

export default AuthRoute;
